package com.firsteapps.mobile.android.puzzle.game;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class PuzzleInfo implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2077889593947243299L;
	private String file;
	private int size = 3;
	private String dateCreated;
	private String alternativeImage;
	private String alternativeText;

	private Point[] tilePositions;
	private int moveCount = 0;
	private long elapsed = 0;
	private int attempts = 0;

	private int maxMoves = 0;
	private long maxTime = 0;
	private int maxAttempts = 0;
	private boolean autosolvable = false;
	private boolean isCompleted = false;
	private String puzzleId;

	private PuzzleInfo() {
	}

	public PuzzleInfo(String file, int size, long maxTime, int maxMoves, int maxAttempts, boolean autosolvable) {
		setFile(file);
		setSize(size);
		setMaxTime(maxTime);
		setMaxMoves(maxMoves);
		setMaxAttempts(maxAttempts);
		setAutosolvable(autosolvable);
		setCreationDate(System.currentTimeMillis());
		setPuzzleId(UUID.randomUUID().toString());
	}

	public String getPuzzleId() {
		return puzzleId;
	}

	private void setPuzzleId(String puzzleId) {
		this.puzzleId = puzzleId;
	}

	public boolean isCompleted() {
		return isCompleted;
	}

	public void setCompleted(boolean isCompleted) {
		this.isCompleted = isCompleted;
	}

	public void resetPuzzle() {
		setMoveCount(0);
		setElapsed(0);
		setAttempts(0);
		setTilePositions(null);
		setCompleted(false);
	}

	public void resetPuzzleAfterRunout() {
		if (increaseAttemptCount()) {
			setAttempts(getAttempts() - 1);
			setMoveCount(0);
			setElapsed(0);
			setCompleted(false);
		}
		setTilePositions(null);
	}

	public String getFile() {
		return file;
	}

	public void setFile(String file) {
		this.file = file;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public String getCreationDate() {
		return dateCreated;
	}

	public void setCreationDate(long currentTimeMillis) {
		DateFormat formatter = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
		this.dateCreated = formatter.format(new Date(currentTimeMillis));
	}

	public String getAlternativeImage() {
		return alternativeImage;
	}

	public void setAlternativeImage(String alternativeImage) {
		this.alternativeImage = alternativeImage;
	}

	public String getAlternativeText() {
		return alternativeText;
	}

	public void setAlternativeText(String alternativeText) {
		this.alternativeText = alternativeText;
	}

	public void setTilePositions(Point[] tilePositions) {
		this.tilePositions = tilePositions;
	}

	public Point[] getTilePositions() {
		return tilePositions;
	}

	public void setMoveCount(int moveCount) {
		this.moveCount = moveCount;
	}

	public int getMoveCount() {
		return moveCount;
	}

	public boolean increaseMoveCount() {
		if (maxMoves < 0 || moveCount < maxMoves) {
			moveCount++;
			return true;
		} else
			return false;
	}

	public boolean addElapsedTime(long l) {
		if (maxTime < 0 || elapsed + l <= maxTime * 1000) {
			elapsed += l;
			return true;
		}
		return false;
	}

	public void setElapsed(long elapsed) {
		this.elapsed = elapsed;
	}

	public long getElapsed() {
		return elapsed;
	}

	public int getAttempts() {
		return attempts;
	}

	public void setAttempts(int attempts) {
		this.attempts = attempts;
	}

	public boolean increaseAttemptCount() {
		if (maxAttempts < 0 || attempts < maxAttempts) {
			attempts++;
			return true;
		}
		return false;
	}

	public int getMaxMoves() {
		return maxMoves;
	}

	public void setMaxMoves(int maxMoves) {
		this.maxMoves = maxMoves;
	}

	public long getMaxTime() {
		return maxTime;
	}

	public void setMaxTime(long maxTime) {
		this.maxTime = maxTime;
	}

	public int getMaxAttempts() {
		return maxAttempts;
	}

	public void setMaxAttempts(int maxAttempts) {
		this.maxAttempts = maxAttempts;
	}

	public boolean isAutosolvable() {
		return autosolvable;
	}

	public void setAutosolvable(boolean autosolvable) {
		this.autosolvable = autosolvable;
	}
}
